package com.atguigu.eduservice.controller;


import com.aiguigu.commonutils.R;
import com.atguigu.eduservice.constant.CustomConstant;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.query.CourseQuery;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-24
 */
@Api(description = "课程控制器")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Resource
    private EduCourseService eduCourseService;


    @ApiOperation("添加课程实体")
    @PostMapping("/saveEntity")
    public R saveEntity(@RequestBody CourseInfoVo courseInfoVo){
        return R.ok().data("info", eduCourseService.saveEntity(courseInfoVo));
    }
    @ApiOperation("根据课程id获取实体信息")
    @GetMapping("/getEntityById/{id}")
    public R getEntityById(@ApiParam("课程id") @PathVariable("id") String id){
        return R.ok().data("info", eduCourseService.getEntityById(id));
    }

    @ApiOperation("根据课程id修改实体信息")
    @PutMapping("/updateEntity")
    public R updateEntity(
            @ApiParam
            @RequestBody CourseInfoVo courseInfoVo){
        String courseId = eduCourseService.updateEntity(courseInfoVo);
        return R.ok().data("info",courseId);
    }
    @ApiOperation("根据课程id获取组合信息")
    @GetMapping("/findCoursePublishVoById/{courseId}")
    public R findCoursePublishVoById(
            @ApiParam("课程id")
            @PathVariable String courseId){
        return R.ok().data(eduCourseService.findCoursePublishVoById(courseId));
    }

    @ApiOperation("根据课程id修改课程状态")
    @GetMapping("/updateStatusById/{courseId}")
    public R updateStatusById(@PathVariable("courseId") String id){
        EduCourse course = eduCourseService.getById(id);
        course.setStatus(CustomConstant.NORMAL);
        return eduCourseService.updateById(course)? R.ok() :R.error();
    }

    @ApiOperation("获取已经发布的课程信息")
    @PostMapping("/listEntity/{page}/{limit}")
    public R listEntity(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQuery courseQuery){
        // 创建分页对象
        Page<EduCourse> eduCoursePage = new Page<>(page, limit);
        // 其他条件
        LambdaQueryWrapper<EduCourse> wrapper = new LambdaQueryWrapper<>();
        // 条件拼接判断
        if (Objects.nonNull(courseQuery)) {
            String title = courseQuery.getTitle();
            String status = courseQuery.getStatus();
            String subjectId = courseQuery.getSubjectId();
            String subjectParentId = courseQuery.getSubjectParentId();
            String teacherId = courseQuery.getTeacherId();
            wrapper
                    .like(StringUtils.hasLength(title), EduCourse::getTitle, title)
                    .eq(!StringUtils.isEmpty(status), EduCourse::getStatus, status)
                    .eq(!StringUtils.isEmpty(subjectId), EduCourse::getSubjectId, subjectId)
                    .eq(!StringUtils.isEmpty(teacherId), EduCourse::getTeacherId, teacherId)
                    .eq(!StringUtils.isEmpty(subjectParentId), EduCourse::getSubjectParentId, subjectParentId);
        }
        wrapper.orderBy(true,false,EduCourse::getPrice);
        eduCourseService.page(eduCoursePage, wrapper);
        List<EduCourse> records = eduCoursePage.getRecords();
        return R.ok().data(records).data("total",eduCoursePage.getTotal());
    }

    /**
     * 根据id删除课程，删除章节，删除小结
     * @param id 主键id
     * @return
     */
    @ApiOperation(value = "根据ID删除实体")
    @DeleteMapping("/deleteEntityById/{id}")
    public R deleteEntityById(
            @ApiParam(name = "id", value = "讲师Id", required = true)
            @PathVariable("id") String id) {
        return  eduCourseService.deleteEntityById(id) ? R.ok() : R.error();
    }
}

